package com.plan.controller.card;

import com.plan.base.constant.BaseCode;
import com.plan.base.constant.Constant;
import com.plan.base.entity.Result;
import com.plan.controller.card.request.BaseReq;
import com.plan.controller.card.request.UpdateLotteryRateReq;
import com.plan.controller.user.response.UserVO;
import com.plan.service.card.PointPrizeDrawSettingService;
import com.plan.service.card.TeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 抽奖设置
 *
 * @author zcl
 */
@Slf4j
@RestController
@RequestMapping("awardSetting/")
public class PointPrizeDrawSettingController {
    @Resource
    private TeamService teamService;
    @Resource
    private PointPrizeDrawSettingService pointPrizeDrawSettingService;

    /**
     * 修改抽奖概率
     */
    @PostMapping(value = "updateLotteryRate")
    public Result<Long> updateLotteryRate(@RequestBody UpdateLotteryRateReq req, HttpServletRequest httpRequest) {
        if (req.getSpendPoint() == null || req.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        req.setUpdateUserId(vo.getId());
        return pointPrizeDrawSettingService.updateLotteryRate(req);
    }

    /**
     * 查询抽奖概率
     */
    @PostMapping(value = "getSpendPoint")
    public Result<Integer> getSpendPoint(@RequestBody BaseReq req, HttpServletRequest httpRequest) {
        if (req.getTeamId() == null) {
            return Result.fail(BaseCode.PARAM_ERROR);
        }
        UserVO vo = (UserVO) httpRequest.getAttribute(Constant.USER_KEY);
        if (!teamService.isUserInTeam(req.getTeamId(), vo.getId())) {
            return Result.fail(BaseCode.ILLEGAL_OPERATION);
        }
        return pointPrizeDrawSettingService.getSpendPoint(req);
    }
}
